﻿using Android.Bluetooth;
using Android.Content;
using Android.OS;
using Java.Util;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SnakeRemote.Services.BluetoothService
{
    public sealed partial class BluetoothService : IBluetoothService
    {
        readonly BluetoothAdapter adapter;
        public bool IsEnabled => adapter is not null && adapter.IsEnabled;
        public IEnumerable<IBluetoothDeviceInfo> GetBondedDevices()
        {
            return adapter.BondedDevices
                .Select(device => new BluetoothDeviceInfo(device));
        }
        public BluetoothConnection CreateConnection(string address, UUID uuid)
        {
            var device = adapter.BondedDevices.FirstOrDefault(
                device => device.Address == address);
            var socket = device.CreateRfcommSocketToServiceRecord(uuid);
            return new(socket);
        }
        public BluetoothService(Context context)
        {
            var manager = (BluetoothManager)context.GetSystemService(Context.BluetoothService);
            adapter = manager.Adapter;
        }
    }
}
